package com.ln.jsoup.demo;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.*;


public class Demo {

    public static final int TITLE_INDEX = 8;

    public static void main(String[] args) throws IOException {

        for (; ; ) {
            System.out.println("输入网址后，等到数据导出～");
            Scanner sc = new Scanner(System.in);
            String page = sc.nextLine();//读取字符串

            if(page == null || page.equals("")) {
                System.out.println("非法不处理...");
                continue;
            }

            if (page.equals("exit")) {
                System.out.println("bye~");
                break;
            }

            try {
//                String page = "https://www.target.com/p/air-freshener-sachet-lemon-38-mint-1oz-everspring-8482/-/A-80887270#lnk=sametab";
//                String page = "https://www.target.com/p/glade-plugins-scented-oil-air-freshener-joyful-citrus-38-daisies-refill-1-34oz-2ct/-/A-81408279#lnk=sametab";
                Document document = Jsoup.connect(page).get();

                Map<Integer, String> reList = crawlPage(document);
                System.out.println(reList);

                exportExcel(reList);
            } catch (IllegalArgumentException e) {
                System.err.println(e.getMessage());
            }
        }
    }


    private static Map<Integer, String> crawlPage(Document document) {
        Map<Integer, String> dataMap = new LinkedHashMap<>();
        // 标题
        dataMap.put(TITLE_INDEX, crawlPageText(document, "标题", "#pageBodyContainer > div:nth-child(1) > div.h-padding-h-default > h1 > span"));

        // Specifications 下面的所有的div 列表
        Elements specificationDivs = document.select("#specAndDescript > div:nth-child(1) > div.styles__StyledCol-sc-fw90uk-0.ktKtbv.h-padding-h-tight > div");
        List<String[]> specificationDivTexts = new ArrayList<>();
        specificationDivs.forEach(el -> {
            String[] texts = el.text().split(":");
            specificationDivTexts.add(texts);
            if (texts[0].equals("Origin")) {
                dataMap.put(10, texts[1]);
            }
        });

        // Origin

        // shop all
        dataMap.put(11, crawlPageText(document, "shop all", "#pageBodyContainer > div:nth-child(1) > div.h-padding-h-default > div.h-margin-b-tiny > ul > li > a > span",
                msg -> msg.substring(msg.indexOf("Shop all") + "Shop all".length())));

        // 描述
        dataMap.put(12, crawlPageText(document, "描述", "#specAndDescript > div:nth-child(1) > div.styles__StyledCol-sc-fw90uk-0.ktKtbv.h-padding-l-default > div.h-margin-v-default"));

        // Highlights 取5个
        Elements highlightsDivs = document.select("#tabContent-tab-Details > div > div > div > div.styles__StyledRow-sc-wmoju4-0.kEpCtb.h-margin-v-default > div > div > ul > div > div");
        int[] index = new int[]{13};
        highlightsDivs.forEach(el -> {
            dataMap.put(index[0]++, el.text());
        });

        // 其他信息
        int[] index2 = new int[]{64};
        specificationDivTexts.forEach(tx -> {
            if (!tx[0].contains("General Disclaimer")) {
                dataMap.put(index2[0]++, tx[0]);
                dataMap.put(index2[0]++, tx[1]);
            }
        });

        // 价格
        dataMap.put(26, "1");
        dataMap.put(27, "1");
        dataMap.put(28, "1");
        dataMap.put(29, "1");
        dataMap.put(30, "1");
        dataMap.put(31, "1");
        dataMap.put(32, crawlPageText(document, "价格", "#pageBodyContainer > div:nth-child(1) > div.styles__StyledRow-sc-wmoju4-0.kEpCtb > div.styles__StyledCol-sc-fw90uk-0.bsDDus.styles__StyledSecondCol-sc-y0ahq4-2.livMpg.h-padding-h-default > div > div.h-margin-b-x2 > div > span > span"));
        dataMap.put(33, "1");

        return dataMap;
    }

    private static void exportExcel(Map<Integer, String> datas) throws IOException {

        Workbook workbook = new XSSFWorkbook();

        Sheet sheet = workbook.createSheet();

        Row row = sheet.createRow(0);

        datas.keySet().forEach(key -> {
            row.createCell(key).setCellValue(datas.get(key));
        });

        File file = new File("." + File.separator + datas.get(TITLE_INDEX).replace("/", "_") + ".xlsx");
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }

        FileOutputStream stream = FileUtils.openOutputStream(file);
        workbook.write(stream);
        stream.close();
    }

    private static String crawlPageText(Document document, String name, String selector) {
        String txt = document.select(selector).text();
//        System.out.println(String.format("%s：%s", name, txt));
        return txt.trim();
    }

    private static String crawlPageText(Document document, String name, String selector, HandleCallback handleCallback) {
        return handleCallback.handle(crawlPageText(document, name, selector)).trim();
    }

    interface HandleCallback {
        String handle(String msg);
    }

}
